﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Fcdbas.Core.Reflection;

namespace Fcdbas.Core.Util
{
    public class ObjectHelper
    {
        /// <summary>
        /// 对象List转换成DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static System.Data.DataTable ObjectTODataTable<T>(List<T> objs)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            TypeX type = TypeX.Create(typeof(T));
            var piList = type.Type.GetProperties().Where(p => p.PropertyType.IsPublic == true).ToList();
            foreach (System.Reflection.PropertyInfo pi in piList)
            {
                PropertyInfoX infoX = PropertyInfoX.Create(pi);
                if(infoX.Member.MemberType == System.Reflection.MemberTypes.Property)
                    dt.Columns.Add(infoX.Name, infoX.Type);
            }
            foreach (T t in objs)
            {
                DataRow dr = dt.NewRow();
                foreach (System.Reflection.PropertyInfo pi in piList)
                {
                    PropertyInfoX infoX = PropertyInfoX.Create(pi);
                    dr[infoX.Name] = infoX.GetValue(t);
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

        /// <summary>
        /// 对象List转换成DataTable
        /// watson 针对报表参数单独处理
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static System.Data.DataTable ObjectTODataTableParam<T>(List<T> objs)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            TypeX type = TypeX.Create(typeof(T));
            var piList = type.Type.GetProperties().Where(p => p.PropertyType.IsPublic == true).ToList();
            foreach (System.Reflection.PropertyInfo pi in piList)
            {
                PropertyInfoX infoX = PropertyInfoX.Create(pi);
                if (infoX.Member.MemberType == System.Reflection.MemberTypes.Property)
                {
                    if ((infoX.Name.ToString().ToUpper() == "PARAMFIELD") || (infoX.Name.ToString().ToUpper() == "PARAMNAME") || (infoX.Name.ToString().ToUpper() == "PARAMLABEL") || (infoX.Name.ToString().ToUpper() == "PARAMREMARK") || (infoX.Name.ToString().ToUpper() == "PARAMVALUE"))
                    {
                        dt.Columns.Add(infoX.Name, infoX.Type);
                    }
                }
            }
            foreach (T t in objs)
            {
                DataRow dr = dt.NewRow();
                foreach (System.Reflection.PropertyInfo pi in piList)
                {
                    PropertyInfoX infoX = PropertyInfoX.Create(pi);
                    if ((infoX.Name.ToString().ToUpper() == "PARAMFIELD") || (infoX.Name.ToString().ToUpper() == "PARAMNAME") || (infoX.Name.ToString().ToUpper() == "PARAMLABEL") || (infoX.Name.ToString().ToUpper() == "PARAMREMARK") || (infoX.Name.ToString().ToUpper() == "PARAMVALUE"))
                    {
                        dr[infoX.Name] = infoX.GetValue(t);
                    }
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }
        public static object CloneObject(object obj)
        {
            using (MemoryStream buffer = new MemoryStream())
            {
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(buffer, obj);
                buffer.Position = 0;
                object temp = formatter.Deserialize(buffer);

                return temp;
            }
        }
        /// <summary>
        /// 序列化DataSet
        /// </summary>
        /// <param name="ds"></param>
        /// <returns></returns>
        public static byte[] SerializeDataSet(DataSet ds)
        {
            //创建内存流
            MemoryStream memStream = new MemoryStream();
            //产生二进制序列化格式
            BinaryFormatter formatter = new BinaryFormatter();
            //指定DataSet串行化格式是二进制
            ds.RemotingFormat = SerializationFormat.Binary;
            //串行化到内存中
            formatter.Serialize(memStream, ds);
            //将DataSet转化成byte[]
            byte[] binaryResult = memStream.ToArray();
            //清空和释放内存流
            memStream.Close();
            memStream.Dispose();
            return binaryResult;
        }
        /// <summary>
        /// DataSet反序列化
        /// </summary>
        /// <param name="binaryData">需要反序列化的byte[]</param>
        /// <returns></returns>
        public static DataSet DeserializeDataSet(byte[] binaryData)
        {
            //创建内存流
            using (MemoryStream memStream = new MemoryStream(binaryData))
            {
                //产生二进制序列化格式
                BinaryFormatter formatter = new BinaryFormatter();
                //反串行化到内存中
                object obj = formatter.Deserialize(memStream);
                //类型检验
                if (obj is DataSet)
                {
                    DataSet dataSetResult = (DataSet)obj;
                    return dataSetResult;
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] SerialzieObject<T>(T obj)
        {
            using (MemoryStream m = new MemoryStream())
            {

                BinaryFormatter bin = new BinaryFormatter();
                bin.Serialize(m, obj);

                return m.ToArray();

            }
        }

        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] SerialzieObject(object obj)
        {
            using (MemoryStream m = new MemoryStream())
            {

                BinaryFormatter bin = new BinaryFormatter();
                bin.Serialize(m, obj);

                return m.ToArray();

            }
        }
        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static T DeserialzieObject<T>(byte[] buffer)
        {
            using (MemoryStream m = new MemoryStream())
            {

                m.Write(buffer, 0, buffer.Length);
                m.Position = 0;

                BinaryFormatter bin = new BinaryFormatter();
                object obj = bin.Deserialize(m);
                if (obj == null)
                    return default(T);
                if (obj is T) return (T)obj;
                return (T)Convert.ChangeType(obj, typeof(T));
               

            }
        }

        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static object DeserialzieObject(byte[] buffer)
        {
            using (MemoryStream m = new MemoryStream())
            {

                m.Write(buffer, 0, buffer.Length);
                m.Position = 0;

                BinaryFormatter bin = new BinaryFormatter();
                object obj = bin.Deserialize(m);
                return obj;


            }
        }
    }
}
